package com.zzzyh.forumsystemclient.cotroller;

import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.SysWebConfig;
import com.zzzyh.forumsystemclient.services.IWebConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api")
@Slf4j
public class WebConfigController {
    @Autowired
    private IWebConfigService webConfigService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping("/webConfig")
    public AppResult<SysWebConfig> webConfig(){
        log.info("获取网站配置");
        return AppResult.success(webConfigService.getWebConfig());
    }
    // 增加网站访问量
    @RequestMapping("/report")
    public AppResult<String> report(){
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String key = "visit_count:" + today;
        log.info("key:"+ key);
        // 增加当日访问量计数
        stringRedisTemplate.opsForValue().increment(key, 1);

        // 设置30天过期时间
        stringRedisTemplate.expire(key, 40, TimeUnit.DAYS);

        return AppResult.success();
    }
}
